package cn.virens.common.boot.provider.apijson;

import apijson.RequestMethod;
import apijson.orm.AbstractObjectParser;
import apijson.orm.AbstractSQLConfig.SimpleCallback;
import apijson.orm.Join;
import apijson.orm.SQLConfig;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class APIJSONParserObject extends AbstractObjectParser<String, JSONObject, JSONArray> {
    private final SimpleCallback<String, JSONObject, JSONArray> SQL_CALLBACK = new SimpleCallback<>() {
        @Override
        public SQLConfig<String, JSONObject, JSONArray> getSQLConfig(RequestMethod method, String database, String schema, String datasource, String table) {
            SQLConfig<String, JSONObject, JSONArray> config = parser.createSQLConfig();

            config.setDatasource(datasource);
            config.setDatabase(database);
            config.setMethod(method);
            config.setSchema(schema);
            config.setTable(table);

            return config;
        }
    };

    public APIJSONParserObject(APIJSONParser parser, JSONObject request, String parentPath, SQLConfig<String, JSONObject, JSONArray> config, boolean isSubquery, boolean isTable, boolean isArrayMainTable) throws Exception {
        super(request, parentPath, config, isSubquery, isTable, isArrayMainTable);

        this.setMethod(parser.getMethod());
        this.setParser(parser);
    }

    @Override
    public SQLConfig<String, JSONObject, JSONArray> newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join<String, JSONObject, JSONArray>> joins, boolean isProcedure) throws Exception {
        return APIJSONSQLConfig.newSQLConfig(method, table, alias, request, joins, isProcedure, SQL_CALLBACK);
    }
}
